using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using System.Diagnostics.CodeAnalysis;
using System.Data.SqlClient;

namespace Net.Fan.SmallPig.Common
{
    public class Config
    {
        private static Config instance;
        //private static string basePath;
        private string directoryOfDatabase, directoryOfUpload, webHostName, shortTitle, webTitle, webDescription, dbConnectionString, directoryOfTemplate;


        private SMTP smtp;
        private bool sSL = false;

        static Config()
        { }

        public Config()
        {
            //DirectoryOfUpload = DirectoryOfDatabase + @"\UploadFiles\";

            SqlConnectionStringBuilder bu = new SqlConnectionStringBuilder();

            bu.DataSource = @".\SQLEXPRESS";
            bu.AttachDBFilename = @"E:\Code\SmallPig\Bug\bin\SmallPig.mdf";
            bu.IntegratedSecurity = true;
            bu.UserInstance = true;

            dbConnectionString = bu.ToString();
            //@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\DEVELOP\SVN_Bug\EnterpriseBug\Bug\bin\EntBugData.mdf;Integrated Security=True;User Instance=True";
            webHostName = @"BugOnline.net";
            smtp = new SMTP();
        }

        public string ShortTitle
        {
            get { return shortTitle; }
            set { shortTitle = value; }
        }

        public string WebTitle
        {
            get { return webTitle; }
            set { webTitle = value; }
        }

        public string WebDescription
        {
            get { return webDescription; }
            set { webDescription = value; }
        }

        public string DirectoryOfDatabase
        {
            get { return directoryOfDatabase; }
            set { directoryOfDatabase = value; }
        }

        public string DirectoryOfUpload
        {
            get { return directoryOfUpload; }
            set { directoryOfUpload = value; }
        }

        public string DirectoryOfTemplate
        {
            get
            {
                if (directoryOfTemplate[directoryOfTemplate.Length - 1] != '\\')
                {
                    directoryOfTemplate = directoryOfTemplate + '\\';
                }
                return directoryOfTemplate;
            }
            set { directoryOfTemplate = value; }
        }

        public string DatabaseMainFilePath
        {
            get
            {
                return this.directoryOfDatabase + @"\SmallPig.mdf";
            }
        }

        public string DatabaseLogFilePath
        {
            get
            {
                return this.directoryOfDatabase + @"\SmallPig_log.ldf";
            }
        }

        public bool SSL
        {
            get { return sSL; }
            set { sSL = value; }
        }

        public SMTP Smtp
        {
            get { return smtp; }
            set { smtp = value; }
        }

        public string WebHostName
        {
            get { return webHostName; }
            set { webHostName = value; }
        }

        public string DatabaseConnectionString
        {
            get { return dbConnectionString; }
            set { dbConnectionString = value; }
        }

        [XmlIgnore]
        public SqlConnectionStringBuilder ConnectionStringBuilder
        {
            get
            {
                return new SqlConnectionStringBuilder(dbConnectionString);
            }
            set
            {
                dbConnectionString = value.ToString();
            }
        }

        public static Config LoadInstanceFromXml(string filePath)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Config));
            XmlTextReader reader = new XmlTextReader(filePath);
            Config config = (Config)(serializer.Deserialize(reader));
            reader.Close();
            return config;
        }

        public void SaveConfig(string path)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Config));
            //string path = typeof(Config).Assembly.FullName;
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Write);
            fs.SetLength(0);
            serializer.Serialize(fs, this);
            fs.Close();
        }

        public static Config Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = LoadInstanceFromXml(BaseConfig.ConfigXmlFilePath); //(Common.Properties.Settings.Default.ConfigPath);
                }
                return instance;
            }
        }

    }

    public class SMTP
    {
        private string credentialPassword;
        private string mailSender;
        private string smtpServer;
        private int smtpPort;
        private string smtpCredential;

        public SMTP()
        {
            credentialPassword = "admin-spms";
            mailSender = "Admin@spms.com";
            smtpServer = "smtp.sohu.com";
            smtpPort = 25;
            smtpCredential = "admin-spms";
        }

        public string SmtpServer
        {
            get { return smtpServer; }
            set { smtpServer = value; }
        }

        public int SmtpPort
        {
            get { return smtpPort; }
            set { smtpPort = value; }
        }

        public string SmtpCredential
        {
            get { return smtpCredential; }
            set { smtpCredential = value; }
        }

        public string CredentialPassword
        {
            get { return credentialPassword; }
            set { credentialPassword = value; }
        }

        public string MailSender
        {
            get { return mailSender; }
            set { mailSender = value; }
        }
    }
}
